//
//  BaseTabBarController.m
//  Project_2_weibo
//
//  Created by 林林 on 16/6/16.
//  Copyright © 2016年 lhn. All rights reserved.
//

#import "BaseTabBarController.h"

@interface BaseTabBarController ()
{
    ThemeImageView *_arrowView;
}

@end

@implementation BaseTabBarController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        //读取storyboard
        NSArray *storyboardNames = @[@"HomeStoryboard",
                                     @"MessageStoryboard",
                                     @"DiscoverStoryboard",
                                     @"ProfileStoryboard",
                                     @"MoreStoryboard"];
        NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
        for (NSString *name in storyboardNames) {
            //根据文件名读取storyboard
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:name bundle:[NSBundle mainBundle]];
            //读取导航控制器
            UIViewController *vc = [storyboard instantiateInitialViewController];
            [viewControllers addObject:vc];
        }
        
        self.viewControllers = viewControllers;
        [self customTabBar];
        
        //添加通知,监听主题切换
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changedTheme) name:kThemeChangedNotificationName object:nil];
        
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)changedTheme {
    
    self.tabBar.backgroundImage = [[ThemeManager sharedManager] themeImageWithImageName:@"mask_navbar"];
}

- (void)customTabBar {
    
    //添加背景图片
//    self.tabBar.backgroundImage = [UIImage imageNamed:@"Skins/cat/mask_navbar"];
    //1 移除系统的TabBar按钮
    for (UIView *view in self.tabBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [view removeFromSuperview];
        }
    }
    //移除tabbar线
    self.tabBar.shadowImage = [[UIImage alloc] init];
    //2 添加自定义按钮
    NSArray *imageNames = @[@"home_tab_icon_1",
                            @"home_tab_icon_2",
                            @"home_tab_icon_3",
                            @"home_tab_icon_4",
                            @"home_tab_icon_5"];
    CGFloat buttonWidth = kScreenWidth/imageNames.count;
    for (int i = 0; i < imageNames.count; i++) {
        //计算frame
        CGRect frame = CGRectMake(i * buttonWidth, 0, buttonWidth, 49);
        ThemeButton *button = [ThemeButton buttonWithType:UIButtonTypeCustom];
        button.frame = frame;
        button.tag = i;
        //设置按钮图片名字
        button.imageName = imageNames[i];
        [self.tabBar addSubview:button];
        //3 添加点击逻辑,改变当前显示页面
        [button addTarget:self action:@selector(tabBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    //选中框
    _arrowView = [[ThemeImageView alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, 49)];
    _arrowView.imageName = @"home_bottom_tab_arrow";
    [self.tabBar insertSubview:_arrowView atIndex:0];
    
}

- (void)tabBarButtonAction:(ThemeButton *)button {
    
    //切换视图
    self.selectedIndex = button.tag;
    //移动选中框
    [UIView animateWithDuration:0.3 animations:^{
        _arrowView.center = button.center;
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
